package com.cfwx.io;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

//import org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory;

public class FileUtil {
	//private static final Log logger = LogFactory.getLog(FileUpLoad.class);
	public static String readFile(String filePathAndName) {
		String fileContent = "";

		File f = new File(filePathAndName);		
			try {
				if (f.isFile() && f.exists()) {
					InputStreamReader read = new InputStreamReader(
							new FileInputStream(f), "gb2312");
					BufferedReader reader = new BufferedReader(read);
					String line;
					while ((line = reader.readLine()) != null) {
						fileContent += line;
					}
					read.close();
				}
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

	
		return fileContent;
	}

	public static void writeFile(String filePath, String fileName, String fileContent) {
		try {
			File f = new File(filePath);
			if (!f.exists()) {
				f.mkdirs();
			}
			File file = new File(filePath + File.separator + fileName);
			System.out.println(filePath + File.separator + fileName);
			OutputStreamWriter write = new OutputStreamWriter(
					new FileOutputStream(file), "UTF-8");
			BufferedWriter writer = new BufferedWriter(write);
			writer.write(fileContent);
			writer.close();
		} catch (Exception e) {
			//logger.error(e.getStackTrace());
			e.printStackTrace();
		}
	}


	/** 
	 * 删除文件夹 
	 * @param filePathAndName String 文件夹路径及名称 如d:/pms/pms.xml  
	 * @param fileContent String 
	 * @return boolean 
	 */
	public static boolean delFolder(String folderPath) {
		boolean ret = false;
		try {
			ret = delFolder(new File(folderPath));
		} catch (Exception e) {
			System.out.println("----------文件夹路径有问题-----------");
			ret = false;
			e.printStackTrace();
		}
		return ret;
	}

	public static boolean delFolder(File file){
		boolean ret = false;
		try {
			if(!file.exists()){
				return false;
			}
			if(!file.isDirectory()){
				return false;
			}
			delAllFile(file); //删除完里面所有内容 
			file.delete(); //删除空文件夹 

		} catch (Exception e) {
			System.out.println("删除文件夹操作出错");
			ret = false;
			e.printStackTrace();
		}
		return ret;
	}
	/** 
	 * 删除文件夹里面的所有文件 
	 * @param path String 文件夹路径 如d:/pms/pms.xml  
	 */
	public static void delAllFile(File file) {
		File[] files = file.listFiles();
		for (int i = 0; i < files.length; i++) {
			if(files[i].isDirectory()){
				delFolder(files[i]);
			} else{
				files[i].delete();
			}
		}
	}

	/** 
	 * 复制单个文件 
	 * @param oldPath String 原文件路径 如：d:/pms/pms.xml  
	 * @param newPath String 复制后路径 如：d:/pmsnew/xx.xml
	 * @return boolean 
	 */
	public static void copyFile(String filePath, String newPath) {
		try {
			int bytesum = 0;
			int byteread = 0;
			File oldfile = new File(filePath);
			if (oldfile.exists()) { //文件存在时 
				InputStream inStream = new FileInputStream(oldfile); //读入原文件 
				FileOutputStream fs = new FileOutputStream(newPath);
				byte[] buffer = new byte[1024];
				while ((byteread = inStream.read(buffer)) != -1) {
					bytesum += byteread; //字节数 文件大小 
					fs.write(buffer, 0, byteread);
				}
				inStream.close();
				fs.close();
			}
		} catch (Exception e) {
			System.out.println("复制单个文件操作出错");
			e.printStackTrace();

		}

	}

	/** 
	 * 复制整个文件夹内容 
	 * @param oldPath String 原文件路径 如：d:/pms 
	 * @param newPath String 复制后路径 如：f:/pmsnew
	 * @return boolean 
	 */
	public static void copyFolder(String oldPath, String newPath) {

		try {
			(new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹 
			File a = new File(oldPath);
			String[] file = a.list();//返回由此路径名所表示的目录中的文件名称所组成的字符串数组
			File temp = null;
			for (int i = 0; i < file.length; i++) {
				if (oldPath.endsWith(File.separator)) {//与系统有关的默认名称分隔符'/'
					temp = new File(oldPath + file[i]);
				} else {
					temp = new File(oldPath + File.separator + file[i]);
					/*//getName:返回由此抽象路径名表示的文件或目录的名称,相当于file[i]
					System.out.println((temp.getName()).toString());*/
				}

				if (temp.isFile()) {
					FileInputStream input = new FileInputStream(temp);//fauther is InputStream
					FileOutputStream output = new FileOutputStream(newPath
							+ File.separator + file[i]);
					byte[] b = new byte[1024 * 5];
					int len;
					while ((len = input.read(b)) != -1) {//从此输入流中将最多 b.length 个字节的数据读入一个字节数组中。
						output.write(b, 0, len);//将指定 b数组中从偏移量 o开始的 len个字节写入此输出流。
					}
					output.flush();//刷新此输出流，并强制将所有已缓冲的输出字节写入该流中
					output.close();
					input.close();
				}
				if (temp.isDirectory()) {//如果是子文件夹 
					copyFolder(oldPath + File.separator + file[i], newPath + File.separator + file[i]);
				}
			}
		} catch (Exception e) {
			System.out.println("复制整个文件夹内容操作出错");
			e.printStackTrace();

		}
	}

	/** 
	 * 移动单个文件到指定目录 
	 * @param oldPath String 如：d:/pms/pms.xml  
	 * @param newPath String 如：d:/pmsnew/
	 */
	public static void mvFile(String filePath, String newPath) {
		copyFile(filePath, newPath);
		delFile(filePath);
	}

	/** 
	 * 移动文件夹到指定目录 
	 * @param oldPath String 如：d:/pms/
	 * @param newPath String 如：d:/pmsnew/  
	 */
	public static void moveFolder(String oldFolderPath, String newFolderPath) {
		copyFolder(oldFolderPath, newFolderPath);
		delFolder(oldFolderPath);
	}
	/**
	 * 删除文件
	 * @param filePath
	 */
	public static void delFile(String filePath){
		delFile(new File(filePath));
	}
	/**
	 * 删除文件
	 * @param file
	 */
	public static void delFile(File file){
		if(file == null){
			return ;
		}
		if(file.exists() && file.isFile()){
			file.delete();
		}
	}

	public static void main(String[] args) {
		FileUtil f1 = new FileUtil();
		//f1.copyFolder("d:/folder", "f:/newfolder");
		f1.readFile("E:/123.txt");

	}

}

